"""
Case Type   : 资源池化
Case Name   : 缩容工具gs_dropnode -U USER -G GROUP -h hostlist  指定-h节点
Create At   : 2024.10
Owner       : chen
Description :
    1.执行缩容，-h指定主节点
    2.执行缩容，-h指定非集群节点
    3.执行缩容，-h指定多ip连接符错误
Expect      :
    1.合理报错
    2.合理报错
    3.合理报错
"""

import os
import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.db_host1 = Node('Standby1DbUser').db_host
        self.db_host2 = Node('Standby2DbUser').db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = '-----step1: 执行缩容，-h指定主节点' \
            ' expect: 合理报错-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; \
            gs_dropnode -U {self.s_user} -G {self.s_user} -h {self.pri_node.db_host}'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(f"""The IP of local host {self.pri_node.db_host} is in the target node list""",
        res, text + '失败')

        text = '-----step2: 执行缩容，-h指定非集群节点 expect: 合理报错-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; \
            gs_dropnode -U {self.s_user} -G {self.s_user} -h 10.10.10.10'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(f'''The IP list of target node: ['10.10.10.10']is not in the current cluster''',
        res, text + '失败')

        text = '-----step3: 执行缩容，-h指定多ip连接符错误 expect: 合理报错-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host1},{self.db_host2}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(f'''A maximum of 1 nodes can be dropped''',
        res, text + '失败')

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')